"""
Pydantic数据模型
"""
from datetime import datetime
from typing import Optional, List, Dict, Any, Literal
from pydantic import BaseModel, Field


# ============ 用户相关模型 ============

class LoginRequest(BaseModel):
    """登录请求"""
    username: str = Field(..., min_length=3, description="用户名，最少3个字符")
    password: str = Field(..., min_length=6, description="密码，最少6个字符")


class User(BaseModel):
    """用户信息"""
    id: int = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    name: str = Field(..., description="显示名称")
    role: str = Field(..., description="角色: admin/operator/viewer")


class LoginResponse(BaseModel):
    """登录响应"""
    user: User
    token: str


# ============ 工位相关模型 ============

class WorkstationConfig(BaseModel):
    """工位配置"""
    location: str = Field(..., description="位置")
    devices: List[str] = Field(..., description="设备列表")
    parameters: Optional[Dict[str, Any]] = Field(None, description="参数配置")


class RealtimeStatus(BaseModel):
    """实时状态"""
    cpu_usage: int = Field(..., ge=0, le=100, description="CPU使用率")
    memory_usage: int = Field(..., ge=0, le=100, description="内存使用率")
    task_progress: int = Field(..., ge=0, le=100, description="任务进度")
    running_time: str = Field(..., description="运行时长")
    completed_tasks: int = Field(..., description="完成任务数")
    error_count: int = Field(..., description="错误次数")


class Workstation(BaseModel):
    """工位信息"""
    id: int = Field(..., description="工位ID")
    name: str = Field(..., description="工位名称")
    status: Literal["idle", "running", "error", "offline"] = Field(..., description="工位状态")
    config: WorkstationConfig = Field(..., description="工位配置")
    realtime_status: Optional[RealtimeStatus] = Field(None, description="实时状态")
    created_at: str = Field(..., description="创建时间")
    updated_at: str = Field(..., description="更新时间")


class WorkstationSearchItem(BaseModel):
    """工位搜索结果项"""
    name: str = Field(..., description="工位名称")
    description: str = Field(..., description="工位描述")
    published_at: str = Field(..., description="发布时间")


class WorkstationSearchResponse(BaseModel):
    """工位搜索响应"""
    total: int = Field(..., description="总数")
    items: List[WorkstationSearchItem] = Field(..., description="搜索结果列表")


class WorkstationListResponse(BaseModel):
    """工位列表响应"""
    total: int = Field(..., description="总数")
    items: List[Workstation] = Field(..., description="工位列表")


class SyncWorkstationRequest(BaseModel):
    """同步工位请求"""
    name: str = Field(..., description="工位名称")


# ============ 工位操作相关模型 ============

class WorkstationActionRequest(BaseModel):
    """工位操作请求"""
    action: Literal["start", "pause", "stop", "reset"] = Field(..., description="操作类型")
    parameters: Optional[Dict[str, Any]] = Field(None, description="操作参数")


class ActionResult(BaseModel):
    """操作结果"""
    message: str = Field(..., description="结果消息")
    details: Optional[Dict[str, Any]] = Field(None, description="详细信息")


class WorkstationActionResponse(BaseModel):
    """工位操作响应"""
    action_id: str = Field(..., description="操作ID")
    workstation_id: int = Field(..., description="工位ID")
    action: str = Field(..., description="操作类型")
    status: str = Field(..., description="执行状态")
    executed_at: str = Field(..., description="执行时间")
    result: ActionResult = Field(..., description="执行结果")


class ActionHistoryItem(BaseModel):
    """操作历史项"""
    action: str = Field(..., description="操作类型")
    status: Literal["success", "failed"] = Field(..., description="执行状态")
    message: str = Field(..., description="操作消息")
    executed_at: str = Field(..., description="执行时间")
    parameters: Optional[Dict[str, Any]] = Field(None, description="操作参数")


class UploadActionsRequest(BaseModel):
    """上传操作历史请求"""
    actions: List[ActionHistoryItem] = Field(..., description="操作历史记录数组")


class UploadActionsResponse(BaseModel):
    """上传操作历史响应"""
    uploaded_count: int = Field(..., description="上传成功数量")
    failed_count: int = Field(..., description="上传失败数量")


class ActionHistory(BaseModel):
    """操作历史记录"""
    id: int = Field(..., description="记录ID")
    action: str = Field(..., description="操作类型")
    status: str = Field(..., description="执行状态")
    message: str = Field(..., description="操作消息")
    executed_at: str = Field(..., description="执行时间")
    executed_by: str = Field(..., description="执行人")


class ActionHistoryResponse(BaseModel):
    """操作历史响应"""
    total: int = Field(..., description="总数")
    items: List[ActionHistory] = Field(..., description="历史记录列表")


# ============ WebSocket相关模型 ============

class CurrentTask(BaseModel):
    """当前任务"""
    id: str = Field(..., description="任务ID")
    name: str = Field(..., description="任务名称")
    progress: int = Field(..., ge=0, le=100, description="任务进度")


class WebSocketStatusData(BaseModel):
    """WebSocket状态数据"""
    workstation_id: int = Field(..., description="工位ID")
    status: Literal["idle", "running", "error", "offline"] = Field(..., description="工位状态")
    cpu_usage: int = Field(..., ge=0, le=100, description="CPU使用率")
    memory_usage: int = Field(..., ge=0, le=100, description="内存使用率")
    task_progress: int = Field(..., ge=0, le=100, description="任务进度")
    running_time: int = Field(..., description="运行时长（秒）")
    completed_tasks: int = Field(..., description="完成任务数")
    error_count: int = Field(..., description="错误次数")
    current_task: Optional[CurrentTask] = Field(None, description="当前任务")


class WebSocketMessage(BaseModel):
    """WebSocket消息"""
    type: Literal["status_update", "alert", "task_complete", "error"] = Field(..., description="消息类型")
    timestamp: str = Field(..., description="时间戳")
    data: Dict[str, Any] = Field(..., description="消息数据")


class AlertData(BaseModel):
    """告警数据"""
    workstation_id: int = Field(..., description="工位ID")
    level: Literal["info", "warning", "error"] = Field(..., description="告警级别")
    message: str = Field(..., description="告警消息")
    details: Optional[Dict[str, Any]] = Field(None, description="详细信息")
